<?php

class Adept_Template_Component_Tag_Validator extends Adept_Template_Tag
{

    protected function generateSetters($var, $writer)
    {
        foreach ($this->getAttributes() as $name => $attribute) {
            if ($name == 'class') {
                continue;
            }
            $writer->writePhp("{$var}->set" . ucfirst($name) . "(");
            $writer->writePhpLiteral($this->getAttributeValue($name));
            $writer->writePhp("); \n");
        }
    }

    /**
     * @param Adept_Template_Writer_Php $writer
     */
    public function generate($writer)
    {
        $var = $writer->generateVar();

        if ($this->hasAttribute('class')) {
            $class = $this->getAttributeValue('class');
        }

        $writer->writePhp("{$var} = new {$class}(); \n");

        $this->generateSetters($var, $writer);
        
        $componentTag = $this->getParent();
        $ref = $componentTag->getElementRefCode();
        
        $writer->writePhp("{$ref}->addValidator({$var});\n");
    }

}
